// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/expression/expr.h>
#include <pollux/type/type.h>

namespace kumo::pollux::exec {
    class FunctionCallToSpecialForm {
    public:
        virtual ~FunctionCallToSpecialForm() {
        }

        /// Returns the output Type of the SpecialForm given the input argument Types.
        /// Throws if the input Types do not match what's expected for the SpecialForm
        /// or if the SpecialForm cannot infer the return Type based on the input
        /// arguments, e.g. Try.
        virtual TypePtr resolveType(const std::vector<TypePtr> &argTypes) = 0;

        /// Given the output Type, the child expresssions, and whether or not to track
        /// CPU usage, returns the SpecialForm.
        virtual ExprPtr constructSpecialForm(
            const TypePtr &type,
            std::vector<ExprPtr> &&compiledChildren,
            bool trackCpuUsage,
            const core::QueryConfig &config) = 0;
    };

    /// Returns the output Type of the SpecialForm associated with the functionName
    /// given the input argument Types. If functionName is not the name of a known
    /// SpecialForm, returns nullptr. Note that some SpecialForms may throw on
    /// invalid arguments or if they don't support type resolution, e.g. Try.
    TypePtr resolveTypeForSpecialForm(
        const std::string &functionName,
        const std::vector<TypePtr> &argTypes);

    /// Returns the SpeicalForm associated with the functionName.  If functionName
    /// is not the name of a known SpecialForm, returns nulltpr.
    ExprPtr constructSpecialForm(
        const std::string &functionName,
        const TypePtr &type,
        std::vector<ExprPtr> &&compiledChildren,
        bool trackCpuUsage,
        const core::QueryConfig &config);

    /// Returns true iff a FunctionCallToSpeicalForm object has been registered for
    /// the given functionName.
    bool isFunctionCallToSpecialFormRegistered(const std::string &functionName);
} // namespace kumo::pollux::exec
